package myservice.android.activities;

import myservice.android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;

/** 
 * Base activity for all MyService activities.
 * Inherits {@link Activity Activity}.
 * 
 */
public abstract class BaseActivity extends Activity {
	
	/** 
	 *  Layout ID in the resources file.
	 *  Must be set in the onCreate() method of the activity before calling super.onCreate();
	 */
	protected int layoutId;
	
	/** 
	 *  Activity title displayed in the title bar
	 */
	protected TextView activityTitleView;
	
	/** 
	 *  Content of the activity title displayed in the title bar
	 *  Must be set in the onCreate() method of the subclass, and before calling super.onCreate().
	 */
	protected String activityTitle = "";	
	
	/** 
	 *  Parameters passed to this activity from another activity.
	 *  These are loaded in the {@link #loadActivityParameters() loadActivityParameters()} method.
	 */
	protected Bundle activityParameters;
	
	/** 
	 *  Here you must assign values to the layoutId and activityTitle fields.
	 *  Invoked from the onCreate() method.
	 */	
    protected abstract void initializeLayoutIdAndTitle();
	
	/** 
	 *  Here you must retrieve all views from the Android layout XML files.
	 *  Invoked from the onCreate() method.
	 */	
    protected abstract void retrieveViews();
    
	/** 
	 *  Here you can retrieve parameters passed to the activity.
	 */	
    protected abstract void loadActivityParameters();    
        
	/** 
	 *  Here you must assign listeners to activity views.
	 *  Invoked from the onCreate() method.  
	 */	
    protected abstract void assignListeners();    
    
	/** 
	 *  Here you must load and set up all views and values of the activity.
	 *  Invoked from the onResume() method.  
	 */	    
    protected abstract void updateUi();	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);
        
        initializeLayoutIdAndTitle();
        
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(layoutId);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
        activityTitleView = (TextView) findViewById(R.id.titleText);
        activityTitleView.setText(activityTitle);
        
        retrieveViews();
        
        activityParameters = getIntent().getExtras();
        
        if (activityParameters != null)
        	loadActivityParameters();
        
        assignListeners();        
        
	}
	
    @Override
    public void onResume() {    	
    	super.onResume();
    	updateUi();    	
    }	                   

}
